<!-- eslint-disable no-undef -->
<script setup lang="ts">
import {
  reqCreateOrder,
  reqGetPatientInfo,
  reqGetPayInfoPre,
  reqPay
} from '@/services/consult'
import type { PayInfoType } from '@/types/consult'
import type { PatientType } from '@/types/patient'
import useStore from '@/stores'
import { showConfirmDialog, showDialog, showToast } from 'vant'
import router from '@/router'
const { consult } = useStore()
// 获取支付信息
const payInfo = ref<PayInfoType>()
const getPayInfoPre = async () => {
  const res = await reqGetPayInfoPre({
    type: consult.consult.type,
    illnessType: consult.consult.illnessType
  })
  // console.log(res)
  payInfo.value = res.data
}

const patientInfo = ref<PatientType>()
// 获取患者信息
const getPatientInfo = async () => {
  const res = await reqGetPatientInfo(consult.consult.patientId as string)
  patientInfo.value = res.data
}
const getInfo = () => {
  // 如果收集信息不完整 提示用户重新填写信息 并回到首页
  if (
    consult.consult.type === undefined ||
    consult.consult.patientId === undefined ||
    consult.consult.consultFlag === undefined ||
    consult.consult.illnessType === undefined
  )
    return showDialog({
      title: '温馨提示',
      message: '支付信息不完整请重新填写，如果未支付可以去记录继续支付'
    }).then(() => {
      router.push('/')
    })
  getPayInfoPre()
  getPatientInfo()
}
getInfo()
// 展示抽屉组件的布尔值
const show = ref(false)
const isAgree = ref(false)
const loading = ref(false)
const orderId = ref<string>('')
// 点击支付按钮生成订单id
const createOrderId = async () => {
  // 1. 同意支付协议
  if (!isAgree.value) return showToast('请同意支付协议')
  loading.value = true
  // 2. 生成订单
  const res = await reqCreateOrder(consult.consult)
  console.log(res.data.id) // 订单id
  orderId.value = res.data.id
  // 3. 展示抽屉
  show.value = true
  // 4. 将consult中收集的数据清空
  consult.clear()
  loading.value = false
}
// 关闭抽屉前的处理函数
const beforeClose = () => {
  // 确认框
  showConfirmDialog({
    title: '关闭支付',
    message: '取消支付将无法获得医生回复，医生接诊名额有限，是否确认关闭？',
    confirmButtonText: '继续支付',
    cancelButtonText: '仍要离开',
    closeOnPopstate: false
  })
    .then(() => {
      return false // 阻止抽屉关闭
    })
    .catch(() => {
      orderId.value = '' // 清空订单id后离开
      router.push('/user/consult')
    })
}
// 支付
// const payMethod = ref<0 | 1 | 2>(0)
const submit = async (payMethod: 0 | 1 | 2) => {
  const res = await reqPay({
    orderId: orderId.value,
    paymentMethod: payMethod,
    payCallback: import.meta.env.VITE_APP_CALLBACK + '/room'
  })
  console.log(res.data.payUrl)
  location.href = res.data.payUrl
}

// 生成订单后不允许离开这个页面
// onBeforeRouteLeave 切换路由地址离开前  return false 阻止路由跳转
onBeforeRouteLeave(() => {
  if (orderId.value) return false
})
</script>
<template>
  <div class="consult-pay-page" v-if="payInfo && patientInfo">
    <cp-nav-bar title="支付" />
    <div class="pay-info">
      <p class="tit">图文问诊 {{ payInfo.payment }} 元</p>
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p class="desc">
        <span>极速问诊</span>
        <span>自动分配医生</span>
      </p>
    </div>
    <van-cell-group>
      <van-cell title="优惠券" :value="`-¥${payInfo.couponDeduction}`" />
      <van-cell title="积分抵扣" :value="`-¥${payInfo.pointDeduction}`" />
      <van-cell
        title="实付款"
        :value="`¥${payInfo.actualPayment}`"
        class="pay-price"
      />
    </van-cell-group>
    <div class="pay-space"></div>
    <van-cell-group>
      <van-cell
        title="患者信息"
        :value="`${patientInfo.name} | ${patientInfo.genderValue} | ${patientInfo.age}岁`"
      ></van-cell>
      <van-cell
        title="病情描述"
        :label="consult.consult.illnessDesc"
      ></van-cell>
    </van-cell-group>
    <div class="pay-schema">
      <van-checkbox v-model="isAgree"
        >我已同意 <span class="text">支付协议</span></van-checkbox
      >
    </div>
    <van-submit-bar
      button-type="primary"
      :price="payInfo.actualPayment * 100"
      button-text="立即支付"
      text-align="left"
      @click="createOrderId"
      :loading="loading"
    />
    <!-- 动作面板  抽屉 -->
    <cp-pay-sheet
      v-model:show="show"
      :actual-payment="payInfo.actualPayment"
      :before-close="beforeClose"
      @submit="submit"
    ></cp-pay-sheet>
  </div>
  <div v-else>
    <van-skeleton title :row="3" />
    <van-skeleton title :row="5" />
    <van-skeleton title :row="3" />
  </div>
</template>

<style lang="scss" scoped>
.consult-pay-page {
  padding: 46px 0 50px 0;
}
.pay-info {
  display: flex;
  padding: 15px;
  flex-wrap: wrap;
  align-items: center;
  .tit {
    width: 100%;
    font-size: 16px;
    margin-bottom: 10px;
  }
  .img {
    margin-right: 10px;
    width: 38px;
    height: 38px;
    border-radius: 4px;
    overflow: hidden;
  }
  .desc {
    flex: 1;
    > span {
      display: block;
      color: var(--cp-tag);
      &:first-child {
        font-size: 16px;
        color: var(--cp-text2);
      }
    }
  }
}
.pay-price {
  ::v-deep() {
    .vam-cell__title {
      font-size: 16px;
    }
    .van-cell__value {
      font-size: 16px;
      color: var(--cp-price);
    }
  }
}
.pay-space {
  height: 12px;
  background-color: var(--cp-bg);
}
.pay-schema {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  .text {
    color: var(--cp-primary);
  }
}
::v-deep() {
  .van-submit-bar__button {
    font-weight: normal;
    width: 160px;
  }
}
</style>
